import {createRouter, createWebHistory} from 'vue-router'
import {useUserStore} from "@/stores/userStore";
import {adminUserStore} from '@/stores/adminStore';

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        //首页
        {
            path: '/',
            name: 'index',
            title: '首页',
            component: () => import('@/pages/ProductsModule/Index.vue'),
        },
        //公用登录注册页面
        {
            path: '/login-register',
            name: 'login-register',
            title: '登录/注册',
            component: () => import('@/pages/Public/LoginRegister.vue'),
        },
        //商品详情
        {
            path: '/product/:id',
            name: 'productDetail',
            component: () => import('@/pages/ProductsModule/ProductDetail.vue'),
            meta: {
                requiresAuth: true,
                role: 'user' // 明确标识用户路由
            }
        },
        //购物车
        {
            path: '/orderPublic',
            name: 'orderPublic',
            component: () => import('@/pages/Public/OrderPublic.vue'),
            meta: {
                requiresAuth: true,
                role: 'user' // 明确标识用户路由
            }
        },
        //我的账户
        {
            path: '/myAccount/:id',
            name: 'myAccount',
            title: '我的账户',
            component: () => import('@/pages/UserModule/MyAccount.vue'),
            meta: {
                requiresAuth: true,
                role: 'user' // 明确标识用户路由
            }
        },
        //我的订单
        {
            path: '/myOrder/:id',
            name: 'myOrder',
            title: '我的订单',
            component: () => import('@/pages/UserModule/MyOrders.vue'),
            meta: {
                requiresAuth: true,
                role: 'user' // 明确标识用户路由
            }
        },
        //支付成功
        {
            path: '/paySuccess',
            name: 'paySuccess',
            title: '支付成功',
            component: () => import('@/pages/OrdersInvoicesModule/PaySuccess.vue'),
            meta: {
                requiresAuth: true,
                role: 'user' // 明确标识用户路由
            }
        },


        /**
         * 管理员模块
         */
        //管理员主页
        {
            path: '/adminDashboard',
            name: 'adminDashboard',
            component: () => import('@/pages/adminModule/public/adminDashboard.vue'),
            meta: {
                title: "管理员面板",
                requiresAuth: true,
                role: 'admin',
            },
            redirect: '/adminProfile',
            children: [
                // 管理员个人信息
                {
                    path: '/adminProfile',
                    name: 'adminProfile',
                    component: () => import('@/pages/adminModule/user/AdminProfile.vue'),
                    meta: {
                        title: "个人信息",
                        requiresAuth: true,
                        role: 'admin',
                    }
                },
                //用户管理
                {
                    path: '/admin',
                    name: 'admin',
                    meta: {
                        title: "用户管理",
                        requiresAuth: true,
                        role: 'admin',
                    },
                    children: [
                        //管理员管理
                        {
                            path: '/adminManagement',
                            name: 'adminManagement',
                            component: () => import('@/pages/adminModule/user/AdminManagement.vue'),
                            meta: {
                                title: "后端管理员",
                                requiresAuth: true,
                                role: 'admin',
                            }
                        },
                        // 用户管理
                        {
                            path: '/userManagement',
                            name: 'userManagement',
                            component: () => import('@/pages/adminModule/user/userManagement.vue'),
                            meta: {
                                title: "商品用户",
                                requiresAuth: true,
                                role: 'admin',
                            },
                        },
                    ]
                },
                // 商品管理
                {
                    path: '/product',
                    name: 'product',
                    meta: {
                        title: "商品管理",
                        requiresAuth: true,
                        role: 'admin',
                    },
                    children: [
                        // 商品管理
                        {
                            path: '/productManagement',
                            name: 'productManagement',
                            component: () => import('@/pages/adminModule/product/ProductManagement.vue'),
                            meta: {
                                title: "所有商品",
                                requiresAuth: true,
                                role: 'admin',
                            },
                        },
                        // 商品类型管理
                        {
                            path: '/productType',
                            name: 'productType',
                            component: () => import('@/pages/adminModule/product/productType.vue'),
                            meta: {
                                title: "商品类型",
                                requiresAuth: true,
                                role: 'admin',
                            },
                        },
                        // 用户所有订单
                        {
                            path: '/userAllOrders',
                            name: 'userAllOrders',
                            component: () => import('@/pages/adminModule/product/UserAllOrders.vue'),
                            meta: {
                                title: "所有订单",
                                requiresAuth: true,
                                role: 'admin',
                            }
                        },
                    ]
                }
            ]
        }
    ],
})


// 全局前置守卫：检查用户是否登录，并根据角色跳转
router.beforeEach(async (to, from, next) => {
    const userStore = useUserStore();
    const adminStore = adminUserStore();

    // 💡 增强校验逻辑
    const checkAuth = (store) => {
        if (!store.checkTokenExpiration()) {
            store.logout()
            return false
        }
        return !!store.isLogin
    }

    // 定义公开路由
    const publicRoutes = ['/', '/login-register'];
    // 检查是否是公开路由
    if (publicRoutes.includes(to.path)) return next();

    // 需要用户登录
    if (to.meta.role === 'user') {
        checkAuth(userStore) ? next() : next('/login-register')
    }
    // 需要管理员登录
    else if (to.meta.role === 'admin') {
        checkAuth(adminStore) ? next() : next('/login-register')
    }
    // 其他情况
    else {
        // 自动识别已登录用户类型
        if (userStore.isLogin && checkAuth(userStore)) {
            return to.matched.some(r => r.meta.role === 'user') ? next() : next('/')
        }
        if (adminStore.isLogin && checkAuth(adminStore)) {
            return to.matched.some(r => r.meta.role === 'admin') ? next() : next('/')
        }
        next('/login-register')
    }
});
export default router
